<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of model_image
 *
 * @author b4by
 */
class model_image extends MY_Model {

	var $table = 'image';

	public function get_list($input = array()) {
		if(!isset($input['select'])){
			$input['select'] = 'i.id,title,upload_date,path,thumb,thumb2,i.views,vote_up,vote_down,report,i.likes,i.comments,fullname,user_id,fb_id,view_freq,i.active,i.new,last,video,suppend';
		}
		if(!isset($input['order'])) $input['order'] = array('i.id','DESC');
		
		$this->get_list_set_input($input);
		$this->db->from('image i');
		$this->db->join('user u','i.user_id=u.id','inner');
		$query = $this->db->get();
		return $query->result();
	}
	public function get_info($id, $field = '') {
		return $this->get_info_rule(array('i.id' => $id));
	}

	public function get_info_rule($where, $field = '') {
		
		if (!$field) {
			$field = 'i.id,title,user_id,fullname,upload_date,path,thumb,thumb2,fb_id,i.views,i.likes,comments,view_freq,i.active,i.new,last,vote_up,vote_down,video,video_id,text_image';
		}
		$input['select'] = $field;
		$input['where'] = $where;
		$input['order'] = array('i.id', 'desc');
		
		$this->get_list_set_input($input);

		$this->db->from('image i');
		$this->db->join('user u', 'i.user_id=u.id', 'left');

		$query = $this->db->get();

		$result = $query->result();

		if ($result)
			return $result[0];

		return FALSE;
	}

	public function create($data) {
		$data['title'] = htmlspecialchars($data['title']);
		$id = parent::create($data);
		$this->load->model('model_user');
		$user = $this->model_user->get_info($data['user_id']);
		if (!$user) {
			$this->model_user->update($data['user_id'], array(
				'uploads' => $user->uploads + 1
			));
		}
		return $id;
	}

	public function update($id, $data) {
		//
		$image = $this->get_info($id);
		$this->load->model('model_user');
		$user = $this->model_user->get_info($image->user_id);
		$update = array();
		if (isset($data['views']) && $data['views'] > 0)
			$update['views'] = $user->views - $image->views + $data['views'];

		if (isset($data['likes']) && $data['likes'] > 0)
			$update['likes'] = $user->likes - $image->likes + $data['likes'];
		//pr($update);die;
		if(!empty($update))
			$this->model_user->update($user->id,$update);
		
		parent::update($id, $data);
		
	}
	
	public function del($id)
	{
		$info = $this->get_info($id);
		$this->load->model('model_user');
		$user = $this->model_user->get_info($info->user_id);
		$user->uploads--;
		$user->likes -= $info->likes;
		$user->views -= $info->views;
		$this->model_user->update($info->user_id,array(
			'uploads'	=> $user->uploads,
			'views'	=> $user->views,
			'likes'	=> $user->likes
		));
		parent::del($id);
	}
	
	public function get_total_likes($user_id)
	{
		$query = $this->db->query('select sum(likes) as likes from image where user_id=' . $user_id);
		$result = $query->result();
		return $result[0]->likes;
	}

}

?>
